package com.quad.innovators.salesease.controller.base;


import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.common.Option;import com.quad.innovators.salesease.model.form.base.WarehouseForm;
import com.quad.innovators.salesease.model.query.base.WarehousePageQuery;
import com.quad.innovators.salesease.model.vo.base.WarehouseVO;
import com.quad.innovators.salesease.service.base.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.List;


@Tag(name = "09.仓库接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/warehouse")
@RequiredArgsConstructor
public class WarehouseController {

    private final WarehouseService warehouseService;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.WAREHOUSE)
    @DataPermission(permissionCode = "base:warehouse:data-permission")
    public Result<PageResultBean<WarehouseVO>> listPaged(WarehousePageQuery queryParams) {
        PageResultBean<WarehouseVO> result = warehouseService.listPaged(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "base:warehouse:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid WarehouseForm form) {
        boolean result = warehouseService.save(form);
        return Result.success(result);
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "code"),
    })
    @FunctionPermission(permissionCode = "base:warehouse:detail")
    @DataPermission(permissionCode = "base:warehouse:data-permission")
    public Result<WarehouseForm> getForm(@PathVariable("code") String code) {
        WarehouseForm formData = warehouseService.getFormData(code);
        return Result.success(formData);
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @FunctionPermission(permissionCode = "base:warehouse:edit")
    @DataPermission(permissionCode = "base:warehouse:data-permission")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated WarehouseForm form) {
        boolean result = warehouseService.update(code, form);
        return Result.success(result);
    }

    @Operation(summary = "修改状态")
    @PutMapping(value = "/{code}/status")
    @Parameters({
            @Parameter(name = "code", description = "用户ID"),
            @Parameter(name = "status", description = "用户状态(1:启用;0:禁用)")
    })
    @FunctionPermission(permissionCode = "base:warehouse:status")
    @DataPermission(permissionCode = "base:warehouse:data-permission")
    public Result<Void> updateUserStatus(@PathVariable("code") String code, @RequestParam("status") Integer status) {
        boolean result = warehouseService.updateStatus(code, status);
        return Result.judge(result);
    }

    @Operation(summary = "下拉列表")
    @GetMapping("/options")
    @DataPermission(permissionCode = "base:warehouse:data-permission")
    public Result<?> listOptions() {
        List<Option<String>> menus = warehouseService.listOptions();
        return Result.success(menus);
    }
}
